﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.Web
Imports Boogaloo.Core.UI

Namespace BLL


    Public Class ThemeCSSStyleSheet
        Inherits ThemeBase
        Implements ICSSStyleSheet


#Region "Constructors"


        Public Sub New(ByVal href As String)
            Me.href = href
        End Sub

        Public Sub New(ByVal href As String, ByVal media As String)
            Me.href = href
            Me.media = media
        End Sub

#End Region

        ''' <summary>
        ''' Returns the xml representation of this object
        ''' </summary>
        Public Function ToXmlString() As String
            Dim req As HttpRequest = HttpContext.Current.Request
            If String.IsNullOrEmpty(media) Then
                Return String.Format("<stylesheet href=""{0}"" />", href)
            Else
                Dim context As HttpContext = HttpContext.Current
                Return String.Format("<stylesheet href=""{0}"" media=""{1}"" />", href, context.Server.HtmlEncode(media))
            End If

        End Function

        ''' <summary>
        ''' Rewrites contents to the stylesheet if contents has changed
        ''' </summary>
        Public Function Save() As Boolean
            If _state = content_state.changed Then
                Return Groove.WriteTextFileContents(path, contents)
            Else
                Return True
            End If

        End Function

#Region "Properties"

        ''' <summary>
        ''' HttpContext.Current
        ''' </summary>
        Private ReadOnly Property context() As HttpContext
            Get
                Return HttpContext.Current
            End Get
        End Property

        ''' <summary>
        ''' Enumeration indicating the state of the locally stored contents
        ''' </summary>
        Private _state As content_state = content_state.empty

        ''' <summary>
        ''' Text content of the stylesheet
        ''' Lazy loaded and then cached if enabled
        ''' </summary>
        Private _contents As String = String.Empty
        Public Property contents() As String Implements ICSSStyleSheet.contents
            Get
                If String.IsNullOrEmpty(_contents) And _state = content_state.empty Then

                    Dim key As String = "boogaloo_manifest_theme_file_" & href.Replace("~", String.Empty).Replace("/", String.Empty)
                    If ThemeBase.ProviderSettings.EnableCaching And ThemeBase.Cache(key) IsNot Nothing Then
                        _contents = CType(ThemeBase.Cache(key), String)
                    Else
                        _contents = Groove.GetTextFileContents(path)
                        ThemeBase.CacheData(key, _contents)
                    End If

                    _state = content_state.loaded
                End If
                Return _contents
            End Get
            Set(ByVal value As String)
                _contents = value
                _state = content_state.changed
            End Set
        End Property

        ''' <summary>
        ''' Virtual path to the stylesheet
        ''' </summary>
        Private _href As String = String.Empty
        Public Property href() As String Implements ICSSStyleSheet.href
            Get
                Return _href
            End Get
            Set(ByVal value As String)
                _href = value
            End Set
        End Property

        ''' <summary>
        ''' stylesheet media attribute
        ''' </summary>
        Private _media As String
        Public Property media() As String Implements ICSSStyleSheet.media
            Get
                Return _media
            End Get
            Set(ByVal value As String)
                _media = value
            End Set
        End Property


        ''' <summary>
        ''' Physical path to the css file
        ''' </summary>
        Public ReadOnly Property path() As String Implements ICSSStyleSheet.path
            Get
                Return context.Server.MapPath(href)
            End Get
        End Property

        ''' <summary>
        ''' Returns the filename portion of the href
        ''' </summary>
        Public ReadOnly Property filename() As String Implements ICSSStyleSheet.filename
            Get
                Return href.Substring(href.LastIndexOf("/") + 1)
            End Get
        End Property




#End Region

        Private Enum content_state
            empty
            loaded
            changed
        End Enum


    End Class


End Namespace